package cs2340.g21.wms;

import java.util.ArrayList;
import java.util.Calendar;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

/**
 * Allows users to filter apply filters to lists of items
 * For filter features see: Filter.java
 */
public class FilterActivity extends FragmentActivity {
    
    private static final String LOG_TAG = "DEBUGGER";
    private Category mCategory = Category.LOST;
    private ArrayList<Item> original;
    private ArrayList<Item> filtered;
    private Coordinator coordinator;
    
    //date info and datepicker ui
    private Calendar calendar = Calendar.getInstance();
    private int startDay = 0;
    private int startMonth = 0; 
    private int startYear = 0; 
    private int endDay = 0;
    private int endMonth = 0;
    private int endYear = 0;
    EditText startText, endText;
    private int currentPicker;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        coordinator = new Coordinator();
        if(getIntent().getExtras() != null){
            coordinator = (Coordinator) getIntent().getExtras().getSerializable("cs2340.g21.wms.coordinator");
            if(getIntent().hasExtra("items")){
                Log.d(LOG_TAG, "using provided list");
                original = (ArrayList<Item>)getIntent().getExtras().get("original");
            }
        }
        populateStartDate(0, -1, 0);
        populateEndDate(0, -1, 0);
    }
    
    public void updateCategory(View view) {
        switch (view.getId()) {
            case R.id.flost_radio:
                mCategory = Category.LOST;
                break;
            case R.id.ffound_radio:
                mCategory = Category.FOUND;
                break;
            case R.id.fdonation_radio:
                mCategory = Category.DONATE;
                break;
        }
    }
    
    public void setStartDate(View view){
        DialogFragment startFrag = new DatePickerFragment();
        currentPicker = 0;
        startFrag.show(getSupportFragmentManager(), "datePicker");
    }
    
    public void setEndDate(View view){
        DialogFragment endFrag = new DatePickerFragment();
        currentPicker = 1;
        endFrag.show(getSupportFragmentManager(), "datePicker");
    }
    
    public void populateStartDate(int day, int month, int year){
        startText = (EditText)findViewById(R.id.startDateText);
        startText.setText(day+"."+(month+1)+"."+year);
    }
    
    public void populateEndDate(int day, int month, int year){
        endText = (EditText)findViewById(R.id.endDateText);
        endText.setText(day+"."+(month+1)+"."+year);
    }
    
    /**
     * Calls the proper functions from Filter and then starts ItemListActivity again
     * after adding the filtered list and original list to the intent
     * @param view
     */
    public void confirm(View view){
        Filter f = new Filter(original);
        if(mCategory == Category.LOST){
            //Log.d(LOG_TAG, "filter lost");
            filtered = (ArrayList<Item>)f.lostFilter();
        }else if(mCategory == Category.FOUND){
            //Log.d(LOG_TAG, "filter found");
            filtered = (ArrayList<Item>)f.foundFilter();
        }else if(mCategory == Category.DONATE){
            //Log.d(LOG_TAG, "filter donate");
            filtered = (ArrayList<Item>)f.donateFilter();
        }
        f.setItems(filtered);
        
        if(startDay != 0 && endDay != 0){
            //Log.d(LOG_TAG, "filter range");
            filtered = (ArrayList<Item>)f.dateRangeFilter(startDay, endDay, startMonth, endMonth, startYear, endYear);
        }else if(startDay != 0 && endDay == 0){
            //Log.d(LOG_TAG, "filter late");
            filtered = (ArrayList<Item>)f.dateLaterFilter(startDay, startMonth, startYear);
        }else if(startDay == 0 && endDay != 0){
            //Log.d(LOG_TAG, "filter early");
            filtered = (ArrayList<Item>)f.dateEarlierFilter(endDay, endMonth, endYear);
        }else{
            //no filter
        }
        Intent intent = getIntent();
        intent.putExtra("original", original);
        intent.putExtra("items", filtered);
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(FilterActivity.this, ItemListActivity.class);
        finish();
        startActivity(intent);
    }
    
    public void cancel(View view){
        Intent intent = getIntent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.putExtra("items", original);
        intent.putExtra("original", original);
        intent.setClass(FilterActivity.this, ItemListActivity.class);
        finish();
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_filter, menu);
        return true;
    }
    
    /**
     * Used to make date picker dialogs. Variable currentPicker determines
     * which data is changed by input to a dialog
     */
    private class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
        boolean fired = false;
        public Dialog onCreateDialog(Bundle savedInstanceState){
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            if(!fired){
                if(currentPicker == 0){
                    Log.d(LOG_TAG, currentPicker + "lel");
                    startDay = dayOfMonth;
                    startMonth = monthOfYear;
                    startYear = year;
                    populateStartDate(startDay, startMonth, startYear);
                }
                if(currentPicker == 1){
                    Log.d(LOG_TAG, currentPicker + "lol");
                    endDay = dayOfMonth;
                    endMonth = monthOfYear;
                    endYear = year;
                    populateEndDate(endDay, endMonth, endYear);
                }
                fired = true;
            }
        }
    }
    
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.putExtra("items", original);
        intent.putExtra("original", original);
        intent.setClass(FilterActivity.this, ItemListActivity.class);
        startActivity(intent);
        finish();
    }

}
